package com.nian.dao.impl;

import com.nian.dao.BaseDao;
import com.nian.dao.UserDao;
import com.nian.pojo.Product;
import com.nian.pojo.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public boolean addUser(User user) {
        boolean result =false;
        //"insert into `user`(`u_name`,`u_pwd`,`u_phone`,`u_realname`)VALUES(?,?,?,?) "
        String sql = "insert into `t_user`(`username`,`password`,`gender`,`email`,`telephone`,`introduce`,`activeCode`,`state`,`role`,`regisTime`)VALUES(?,?,?,?,?,?,?,?,?,?)";
        int count = update(sql, user.getUsername(), user.getPassword(), user.getGender(),user.getEmail(),user.getTelephone(),user.getIntroduce(),user.getActiveCode(),user.getState(),user.getRole(),user.getregisTime());
        if (count>0){
            result = true;
        }
        return result;
    }

    @Override
    public boolean dlUser(User user) {
        boolean result =false;
        //"DELETE FROM `house` WHERE `h_id`= ?";
        String sql = "DELETE FROM `t_user` WHERE `id` = ?";
        int count = update(sql, user.getId());
        if (count>0){
            result = true;
        }
        return result;

    }


    @Override
    public boolean reUser(User user) {
        boolean result =false;
        String sql = "UPDATE `T_user` SET `username` = ?, `password` = ?, `gender` = ?, `email` = ?, `telephone` = ?, `introduce` = ?,`activeCode` = ?, `state` = ?, `role` = ?, `regisTime` = ?  WHERE `id` = ? ";
        int count = update(sql, user.getUsername(), user.getPassword(), user.getGender(), user.getEmail(), user.getTelephone(), user.getIntroduce(), user.getActiveCode(), user.getState(), user.getRole(), user.getregisTime(), user.getId());;
        if (count>0){
            result = true;
        }
        return result;
    }

    @Override
    public User selectUserById(User user) {
        String sql = "select * from `t_user` where `id` = ?";
        return queryForOne(User.class, sql, user.getId());
    }

    @Override
    public User selectUserByUsernameAndPassword(User user) {
        String sql = "SELECT * FROM `T_user` WHERE `username` = ? AND `password` = ?";
        return queryForOne(User.class, sql, user.getUsername(), user.getPassword());
    }

    @Override
    public List<User> getAllUser() {
        String sql = "SELECT * FROM `T_user`";
        return super.queryForList(User.class, sql);
    }

    @Override
    public User selectUserByUsername(User user) {
        String sql = "SELECT * FROM `T_user` WHERE `username` = ?";
        return queryForOne(User.class, sql, user.getUsername());
    }

}
